package com.fc.mall.portal.service.impl;

import com.fc.mall.mapper.SmsFriendsCirclesMapper;
import com.fc.mall.model.SmsFriendsCircles;
import com.fc.mall.model.SmsFriendsCirclesExample;
import com.fc.mall.portal.domain.SmsFriendsCirclesDto;
import com.fc.mall.portal.service.SmsFriendsCirclesService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Sunny
 * Created by sunny on 2019/10/15.
 */
@Service
public class SmsFriendsCirclesServiceImpl implements SmsFriendsCirclesService {

    @Autowired
    private SmsFriendsCirclesMapper friendsCirclesMapper;

    @Override
    public List<SmsFriendsCirclesDto> getList() {
        SmsFriendsCirclesExample example = new SmsFriendsCirclesExample();
        example.createCriteria();
        example.setOrderByClause("id desc");
        List<SmsFriendsCircles> friendsCirclesList = friendsCirclesMapper.selectByExample(example);
        List<SmsFriendsCirclesDto> friendsCirclesDtoList = friendsCirclesList.stream().map(friendsCircles ->{
            List<String> urls = initUrls(friendsCircles);;
            SmsFriendsCirclesDto friendsCirclesDto = SmsFriendsCirclesDto.builder()
                    .title(friendsCircles.getTitle())
                    .content(friendsCircles.getContent())
                    .picUrls(urls)
                    .build();
            return friendsCirclesDto;
        }).collect(Collectors.toList());
        return friendsCirclesDtoList;
    }


    private List<String> initUrls(SmsFriendsCircles friendsCircles) {
        List<String> urls = Lists.newArrayList();
        if(!StringUtils.isEmpty(friendsCircles.getPic1()))
        {
            urls.add(friendsCircles.getPic1());
        }
        if(!StringUtils.isEmpty(friendsCircles.getPic2()))
        {
            urls.add(friendsCircles.getPic2());
        }
        if(!StringUtils.isEmpty(friendsCircles.getPic3()))
        {
            urls.add(friendsCircles.getPic3());
        }
        if(!StringUtils.isEmpty(friendsCircles.getPic4()))
        {
            urls.add(friendsCircles.getPic4());
        }
        if(!StringUtils.isEmpty(friendsCircles.getPic5()))
        {
            urls.add(friendsCircles.getPic5());
        }
        if(!StringUtils.isEmpty(friendsCircles.getPic6()))
        {
            urls.add(friendsCircles.getPic6());
        }
        if(!StringUtils.isEmpty(friendsCircles.getPic7()))
        {
            urls.add(friendsCircles.getPic7());
        }
        if(!StringUtils.isEmpty(friendsCircles.getPic8()))
        {
            urls.add(friendsCircles.getPic8());
        }
        if(!StringUtils.isEmpty(friendsCircles.getPic9()))
        {
            urls.add(friendsCircles.getPic9());
        }
        return urls;
    }
}
